<?php

class transactionModel extends Model
{

	public static function getById ( $id )
	{

		$db = &DB::getInstance ( require './config/db_config.php' ) ;

		$params = array (
				'id' => array (
						'value' => intval ( $id ),
						'type' => PDO::PARAM_INT
				)
		) ;
			
		$db->query ( 'SELECT * FROM transaction WHERE trs_id=:id', $params ) ;
			
		$result = $db->fetchAll ( ) ;

		if ( count ( $result ) === 0 )
			return null ;
		else
			return new transactionModel ( $result[0], true ) ;

	}
	
	public static function getByAccount ( $id )
	{
	
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
	
		$params = array (
				'id' => array (
						'value' => intval ( $id ),
						'type' => PDO::PARAM_INT
				)
		) ;
			
		$db->query ( 'SELECT * FROM transaction WHERE cmp_id=:id', $params ) ;
			
		$result = $db->fetchAll ( ) ;
	
		if ( count ( $result ) === 0 )
			return null ;
		else
		{
			
			$res = array ( ) ;
			
			foreach ( $result as $transaction )
			{
				
				$res[] = new transactionModel ( $transaction, true ) ;
				
			}
			
			return $res ;
			
		}
	
	}

	public function save ( )
	{
		
		if ( ! isset ( $this->data['trs_date'] ) && ! isset ( $this->data['trs_montant'] ) && ! isset ( $this->data['trs_intitule'] ) && ! isset ( $this->data['cli_id'] ) )
			throw new Exception ( 'Tous les champs du modèle ne sont pas renseignés.' ) ;
		elseif ( ! $this->already_saved )
		{
				
			$params = array (
					'date' => array (
							'value' => strval ( $this->data['trs_date'] ),
							'type' => PDO::PARAM_STR
					),
					'amount' => array (
							'value' => floatval ( $this->data['trs_montant'] ),
							'type' => PDO::PARAM_INT
					),
					'entitled' => array (
							'value' => strval ( $this->data['trs_intitule'] ),
							'type' => PDO::PARAM_STR
					),
					'account_id' => array (
							'value' => intval ( $this->data['cmp_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;

			$this->db->query ( 'INSERT INTO Transaction (trs_date, trs_montant, trs_intitule, cmp_id) VALUES ( :date, :amount, :entitled, :account_id )', $params ) ;
			$this->data['trs_id'] = $this->db->lastId ( ) ;
				
			$this->already_saved = true ;
			$this->modified = false ;
				
		}
		else if ( $this->modified )
		{
				
			$params = array (
					'id' => array (
							'value' => strval ( $this->data['trs_id'] ),
							'type' => PDO::PARAM_INT
					),
					'date' => array (
							'value' => strval ( $this->data['trs_date'] ),
							'type' => PDO::PARAM_STR
					),
					'amount' => array (
							'value' => floatval ( $this->data['trs_montant'] ),
							'type' => PDO::PARAM_INT
					),
					'entitled' => array (
							'value' => strval ( $this->data['trs_intitule'] ),
							'type' => PDO::PARAM_STR
					),
					'account_id' => array (
							'value' => intval ( $this->data['cmp_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;
				
			$this->db->query ( 'UPDATE Transaction SET trs_date=:date, trs_montant=:amount, trs_intitule=:entitled, cmp_id=:account_id WHERE trs_id=:id', $params ) ;
				
			$this->modified = false ;
				
		}

	}

	public function delete ( )
	{

		if ( $this->already_saved )
		{
			
			$params = array (
					'id' => array (
							'value' => intval ( $this->data['trs_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;
			
			$this->db->query ( 'DELETE FROM Transaction WHERE trs_id=:id', $params ) ;
			
			$this->already_saved = false ;
			
		}

	}

	public function getId ( ) { return $this->data['trs_id'] ; }
	public function getDate ( ) { return $this->data['trs_date'] ; }
	public function getAmount ( ) { return $this->data['trs_montant'] ; }
	public function getEntitled ( ) { return $this->data['trs_intitule'] ; }
	public function getAccount ( ) { return $this->data['cmp_id'] ; }
	
	public function setDate ( $arg ) { $this->data['trs_date'] = $arg ; $this->modified = true ; }
	public function setAmount ( $arg ) { $this->data['trs_montant'] = $arg ; $this->modified = true ; }
	public function setEntitled ( $arg ) { $this->data['trs_intitule'] = $arg ; $this->modified = true ; }
	public function setAccount ( $arg ) { $this->data['cmp_id'] = $arg ; $this->modified = true ; }
	
}

?>